package com.nhjf.mobile.controller.recentlyStudied;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.RecentlyStudied;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.RecentlyStudiedQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.RecentlyStudiedService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/rsd")
@Controller
public class RecentlyStudiedController {

	@Autowired
	private RecentlyStudiedService recentlyStudiedService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeachersService teacherService; 

	@RequestMapping("/rsd_query")
	@ResponseBody
	public String rsdQuery(@RequestParam(value="token", required=false) String token,@RequestParam(value="page", defaultValue="1")Integer page) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					RecentlyStudiedQo recentlyStudiedQo=new RecentlyStudiedQo();
					recentlyStudiedQo.setSeachCourses(true);
					recentlyStudiedQo.setSeachTeacher(true);
					recentlyStudiedQo.setSeachUser(true);
					recentlyStudiedQo.setUserId(user.getId());
					Pagination pagination = new Pagination();
					pagination.setPageSize(10);
					pagination.setPageNo(page);
					pagination.setCondition(recentlyStudiedQo);
					pagination = recentlyStudiedService.queryPagination(pagination);
					List<RecentlyStudied> rsdList=(List<RecentlyStudied>)pagination.getList();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					for (RecentlyStudied recentlyStudied : rsdList) {
						recentlyStudied.getUser().setUserPassword("");
						if(recentlyStudied.getTeacher()!=null) {
							TeachersQo teachersQo=new TeachersQo();
							teachersQo.setTeacherId(recentlyStudied.getTeacher().getId());
							teachersQo.setSeachUser(true);
							Teacher teacher= teacherService.queryUnique(teachersQo);
							recentlyStudied.getTeacher().setUserId(teacher.getUser().getId());
							if(!StringUtils.isEmpty(recentlyStudied.getTeacher().getAvatar())) {
								if(recentlyStudied.getTeacher().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									recentlyStudied.getTeacher().setAvatar(imagePath+"/picture/"+recentlyStudied.getTeacher().getAvatar());
								}
							}


						}
						if(!StringUtils.isEmpty(recentlyStudied.getCourses().getCover())) {
							if(recentlyStudied.getCourses().getCover().indexOf(imagePath+"/img/")>=0) {

							}else {
								recentlyStudied.getCourses().setCover(imagePath+"/img/"+recentlyStudied.getCourses().getCover());
							}
							
						}
					}
					if(rsdList!=null) {
						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "406");
						resMap.put("message", "最近没有看课程！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
}
